//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Trigeorgis.h"
using namespace Cephei::QL::Methods::Lattices;
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (Cephei::QL::IStochasticProcess1D^ process, Double end, UInt64 steps, Double strike) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CStochasticProcess1D^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phTrigeorgis = NULL;
#endif
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::StochasticProcess1D>& _process = static_cast<boost::shared_ptr<QuantLib::StochasticProcess1D>&> (_Cprocess->GetShared ()); 
        QuantLib::Time _end = (QuantLib::Time)ValueHelper::Convert (end); //d
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps); //d
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _ppTrigeorgis = new boost::shared_ptr<QuantLib::Trigeorgis> (new QuantLib::Trigeorgis ( _process,  _end,  _steps,  _strike ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (boost::shared_ptr<QuantLib::Trigeorgis>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrigeorgis = NULL;
#endif
	_ppTrigeorgis = &childNative;
    
}
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (QuantLib::Trigeorgis& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTrigeorgis = NULL;
#endif
	_ppTrigeorgis = new boost::shared_ptr<QuantLib::Trigeorgis> (&childNative);
    
    _TrigeorgisOwner = owner;
    
}

Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (CTrigeorgis^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrigeorgis = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTrigeorgis = new boost::shared_ptr<QuantLib::Trigeorgis> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrigeorgis = NULL;
#endif
	if (!t->IsSubclassOf(CTrigeorgis::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (QuantLib::Handle<QuantLib::Trigeorgis>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTrigeorgis = &childNative;
	_ppTrigeorgis = &static_cast<boost::shared_ptr<QuantLib::Trigeorgis>>(childNative.currentLink());
    
    _TrigeorgisOwner = owner;
}
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (QuantLib::Handle<QuantLib::Trigeorgis> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTrigeorgis = &childNative;
	_ppTrigeorgis = &static_cast<boost::shared_ptr<QuantLib::Trigeorgis>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Lattices::CTrigeorgis::CTrigeorgis (QuantLib::Trigeorgis childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrigeorgis = NULL;
#endif
	_ppTrigeorgis = new boost::shared_ptr<QuantLib::Trigeorgis> (new QuantLib::Trigeorgis (childNative));
    
}
#endif

Cephei::QL::Methods::Lattices::CTrigeorgis::~CTrigeorgis ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTrigeorgis != NULL)
    {
	    delete _ppTrigeorgis;
        _ppTrigeorgis = NULL;
    }
}
Cephei::QL::Methods::Lattices::CTrigeorgis::!CTrigeorgis ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTrigeorgis != NULL)
    {
	    delete _ppTrigeorgis;
    }
}
QuantLib::Trigeorgis& Cephei::QL::Methods::Lattices::CTrigeorgis::GetReference ()
{
    if (_ppTrigeorgis == NULL) throw REFNEW NativeNullException ();
	return **_ppTrigeorgis;
}
boost::shared_ptr<QuantLib::Trigeorgis>& Cephei::QL::Methods::Lattices::CTrigeorgis::GetShared ()
{
    if (_ppTrigeorgis == NULL) throw REFNEW NativeNullException ();
	return *_ppTrigeorgis;
}
QuantLib::Trigeorgis* Cephei::QL::Methods::Lattices::CTrigeorgis::GetPointer ()
{
    if (_ppTrigeorgis == NULL) throw REFNEW NativeNullException ();
	return &**_ppTrigeorgis;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Trigeorgis>& Cephei::QL::Methods::Lattices::CTrigeorgis::GetHandle ()
{
	if (_phTrigeorgis == NULL)
	{
		_phTrigeorgis = new Handle<QuantLib::Trigeorgis> (*_ppTrigeorgis);
	}
	return *_phTrigeorgis;
}
#endif
bool Cephei::QL::Methods::Lattices::CTrigeorgis::HasNative () 
{
	return (_ppTrigeorgis != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Lattices::ITrigeorgis^ Cephei::QL::Methods::Lattices::CTrigeorgis_Factory::Create (Cephei::QL::IStochasticProcess1D^ process, Double end, UInt64 steps, Double strike)
{
    return REFNEW CTrigeorgis ( process,  end,  steps,  strike);
}
